package net.homeip.net.tinframework.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: Roger Chu
 * @category: tinFramework
 * 
 * Utility to handle client cookie related operations.
 * 
 */
public class CookieUtil {
	/**
	 * Description - This method returns the cookie value for a given name.
	 * 
	 * @param request
	 * @param cookieName
	 * @return The Cookie
	 */
	public static String getCookie(HttpServletRequest request, String cookieName) {
		String cookieValue = null;

		if ((request != null) && (cookieName != null)) {
			Cookie[] cookies = request.getCookies();

			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					if (cookieName.equalsIgnoreCase(cookies[i].getName())) {
						cookieValue = cookies[i].getValue();
						break;
					}
				}
			}
		}

		return cookieValue;
	}

	/**
	 * This method sets a cookie for a given number of day. A negative value
	 * indicates session cookie and 0 deletes cookie
	 * 
	 * @param response
	 * @param cookieName
	 * @param cookieValue
	 * @param dayExpires
	 * @return True if successful, false if there is illegal character in cookie
	 *         name
	 */
	public static boolean setCookie(HttpServletResponse response,
			String cookieName, String cookieValue, int dayExpires) {
		
		return setCookie( response, cookieName, cookieValue, dayExpires, null, false);
		
	}

	/**
	 * This method sets a session cookie. Returns - true if successful, false if there is illegal character
	 * 
	 * @param response
	 * @param cookieName
	 * @param cookieValue
	 * @return True if successful, false if there is illegal character.
	 */
	public static boolean setSessionCookie(HttpServletResponse response,
			String cookieName, String cookieValue) {
		return setCookie(response, cookieName, cookieValue, -1);
	}

	/**
	 * This method deletes a cookie.
	 * 
	 * @param response
	 * @param cookieName
	 * @return True if successful, false if there is illegal character in cookie name
	 */
	public static boolean deleteCookie(HttpServletResponse response,
			String cookieName) {
		return setCookie(response, cookieName, null, 0);
	}
	
	
	/**
	 * This method sets a cookie for a given number of day. A negative value
	 * indicates session cookie and 0 deletes cookie
	 * 
	 * @param response
	 * @param cookieName
	 * @param cookieValue
	 * @param dayExpires
	 * @param domainName
	 * @return True if successful, false if there is illegal character in cookie
	 *         name
	 */
	public static boolean setCookie(HttpServletResponse response,
			String cookieName, String cookieValue, int dayExpires, String domainName, boolean secured) {
		try {
			Cookie cookie = new Cookie(cookieName, cookieValue);
			cookie.setPath("/");
			cookie.setMaxAge(dayExpires * 24 * 60 * 60);
			if (domainName != null)
			{
				cookie.setDomain(domainName);
			}
			if (secured)
			{
				cookie.setSecure(secured);
			}
			response.addCookie(cookie);
		} catch (java.lang.IllegalArgumentException iae) {
			return false;
		}

		return true;
	}
}
